#include "SkyPropertiesUI.h"
#include "ui_SkyPropertiesUI.h"
#include <QDebug>
#include <cmath>

SkyPropertiesUI::SkyPropertiesUI(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::SkyPropertiesUI)
{
    m_ui->setupUi(this);
}

SkyPropertiesUI::~SkyPropertiesUI()
{
    delete m_ui;
}

void SkyPropertiesUI::uiChanged()
{
	m_ui->azimuthSpinBox->blockSignals(true);
	while(m_ui->azimuthSpinBox->value() < 0)
		m_ui->azimuthSpinBox->setValue(m_ui->azimuthSpinBox->value()+360);
	while(m_ui->azimuthSpinBox->value() >= 360)
		m_ui->azimuthSpinBox->setValue(m_ui->azimuthSpinBox->value()-360);
	m_ui->azimuthSpinBox->blockSignals(false);

	float elevation = (m_ui->elevationSpinBox->value()-90.0f)*3.14159265/180.0 ;
	float azimuth = (m_ui->azimuthSpinBox->value()+90.0f)*3.14159265/180.0;

	float sx = sinf(azimuth);
	float sy = cosf(azimuth);
	float sz = cosf(elevation);

	geom::Vector3D sunVector(-sx,-sz,-sy);


	float skyBrightness = m_ui->skyBrightnessSpinBox->value();
	QColor topColorQ = m_ui->topColorFrame->currentColor();
	geom::Vector3D topColor(topColorQ.redF(),topColorQ.greenF(), topColorQ.blueF());
	QColor bottomColorQ = m_ui->bottomColorFrame->currentColor();
	geom::Vector3D bottomColor(bottomColorQ.redF(),bottomColorQ.greenF(), bottomColorQ.blueF());
	QColor hazeColorQ;// = m_ui->hazeColorFrame->currentColor();
	geom::Vector3D hazeColor(hazeColorQ.redF(),hazeColorQ.greenF(), hazeColorQ.blueF());
	float sunCoverAngle = m_ui->sunSizeSpinBox->value()*3.14159265/180.0;
	float haloCoverAngle = m_ui->sunHaloSpinBox->value()*3.14159265/180.0;
	if(haloCoverAngle <= sunCoverAngle)
	{
		m_ui->sunHaloSpinBox->blockSignals(true);
		m_ui->sunHaloSpinBox->setValue(m_ui->sunSizeSpinBox->value()+0.1);
		m_ui->sunHaloSpinBox->blockSignals(false);
		haloCoverAngle = m_ui->sunHaloSpinBox->value()*3.141592/180.0;
	}
	float sunIntensity = m_ui->sunBrightnessSpinBox->value();
	float sunSmooth = m_ui->sunSmoothnessSpinBox->value();
	float hazeStrength;// = m_ui->hazeStrengthSpinBox->value();
	float hazeAmmount;// = m_ui->hazeAmmountSpinBox->value();

	slotsdata::SkyProperties props(skyBrightness,sunVector.getNormalized(),topColor,bottomColor,hazeColor,
								   sunCoverAngle,haloCoverAngle,
								   sunIntensity,sunSmooth,
								   hazeStrength,hazeAmmount);
	emit(skyPropertiesChanged(props));
}

void SkyPropertiesUI::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
